
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
class-autobind
Advanced tools
This package provides a single function, autobind
, for use within a constructor to bind all methods to the instance itself.
For example, this allows us to pass a method to an event handler element.addEventListener('click', this.onClick)
and be sure the onClick
method will always be called with the right context.
Note: This has some specific logic for React, but could be used in any project.
import autobind from 'class-autobind';
class MyComponent extends React.Component {
constructor() {
super(...arguments);
autobind(this);
}
render() {
return <button onClick={this.onClick}>Click Me</button>;
}
onClick() {
console.log('Button Clicked');
}
}
If your component will possibly be subclassed (you really should not do this, but some third-party libraries like react-css-modules do so) then you will need to specify which prototype will be the source of methods that are to be automatically bound.
import autobind from 'class-autobind';
class MyComponent extends React.Component {
constructor() {
super(...arguments);
autobind(this, MyComponent.prototype); // Note the second parameter.
}
render() {
/* ... */
}
}
class MySubClassedComponent extends MyComponent {
/* This is probably a very bad idea. */
}
This software is BSD Licensed.
FAQs
Method auto-bind for ES6 (ES2015) classes
The npm package class-autobind receives a total of 46,300 weekly downloads. As such, class-autobind popularity was classified as popular.
We found that class-autobind demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.